<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
            <a-col :span="24">
                <a-form-item label="序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-input-number v-decorator="['fsSjXh',{ rules: [{ required: true, message: '请输入时间序号！' }] }]" placeholder="请输入时间序号" style="width: 100%" />
                </a-form-item>
            </a-col>
            <a-col :span="24">
                <a-form-item label="年度" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <!-- :default-value="moment(new Date(), 'YYYY')"  -->
                    <a-date-picker 
                        style="width:100%"
                        :open='yearShowOne' 
                        @openChange="openChangeOne" 
                        @panelChange="panelChangeOne"  
                        mode="year"
                        format="YYYY"
                        type="date" 
                        suffix-icon=" " 
                        disabled
                        placeholder="年份"
                        v-decorator="['shijian',{ initialValue:moment(new Date(), 'YYYY') },]"
                    />
                    <!-- { rules: [{ required: true, message: '请选择年份！' }] }, -->
                </a-form-item>
            </a-col>
            <a-col :span="24">
                <a-form-item label="答辩日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <!-- <a-input v-decorator="['fsSjRq']" placeholder="请输入日期"  ></a-input> -->
                    <a-date-picker v-decorator="['fsSjRq',{ rules: [{ required: true, message: '请选择答辩日期！' }] }]"/>
                </a-form-item>
            </a-col>
            <a-col :span="24">
                <a-form-item label="上下午" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <!-- <a-input v-decorator="['fsSjSxw']" placeholder="请输入上下午"  ></a-input> -->
                    <a-select v-decorator="['fsSjSxw',{ rules: [{ required: true, message: '请选择上下午！' }] }]" placeholder="请选择上下午" style="width:200px;">
                        <a-select-option value="上午">上午</a-select-option>
                        <a-select-option value="下午">下午</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

import moment from 'moment'
  export default {
    name: 'CxjFsSjForm',
    components: {
    },
    props: {
        //流程表单data
        formData: {
            type: Object,
            default: ()=>{},
            required: false
        },
        //表单模式：true流程表单 false普通表单
        formBpm: {
            type: Boolean,
            default: false,
            required: false
        },
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data () {
        return {
            /* 年份参数开始 */
            yearShowOne:false,
            shijian: moment(new Date()).format('YYYY'),
            /* 年份参数结束 */
            form: this.$form.createForm(this),
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            validatorRules: {},
            url: {
                add: "/cxj/cxjFsSj/cxjfs_dbsjAdd",
                edit: "/cxj/cxjFsSj/cxjfs_dbsjEdit",
                queryById: "/cxj/cxjFsSj/queryById"
            }
        }
    },
    computed: {
        formDisabled(){
            if(this.formBpm===true){
                if(this.formData.disabled===false){
                    return false
                }
                return true
            }
            return this.disabled
        },
        showFlowSubmitButton(){
            if(this.formBpm===true){
                if(this.formData.disabled===false){
                    return true
                }
            }
            return false
        }
    },
    created () {
        //如果是流程中表单，则需要加载流程表单data
        this.showFlowData();
        
    },
    methods: {
        moment,
         /* 年份控制开始 */
        openChangeOne(status){
            //status是打开或关闭的状态
            if(status){
                this.yearShowOne =  true;
            }
        },
        // 得到年份选择器的值
        panelChangeOne(value){
            console.log('-----',value)
            // console.log(this.form.setFieldsValue('shijian',moment(value).format('YYYY')))
            this.form.setFieldsValue({
                'shijian': moment(value).format('YYYY')
            })
            // this.shijian = moment(value).format('YYYY');
            // this.queryParam.shijian = this.shijian;
            this.yearShowOne = false
        },
        /* 年份控制结束 */
        add () {
            this.edit({});
        },
        edit (record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model,'fsSjXh','fsSjBh','fsSjRq','fsSjSxw','shijian','isValid','bz1','bz2'))
            })
        },
        //渲染流程表单数据
        showFlowData(){
            if(this.formBpm === true){
                let params = {id:this.formData.dataId};
                getAction(this.url.queryById,params).then((res)=>{
                    if(res.success){
                        this.edit (res.result);
                    }
                });
            }
        },
        submitForm () {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    // that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if(!this.model.id){
                        httpurl+=this.url.add;
                        method = 'post';
                    }else{
                        httpurl+=this.url.edit;
                        method = 'put';
                    }
                    console.log('values===', values)
                    let formData = Object.assign(this.model, values);
                    formData.fsSjRq = moment(values.fsSjRq).format('YYYY-MM-DD')
                    formData.shijian = moment(values.shijian).format('YYYY')
                    let fsSjRqYear = moment(values.fsSjRq).format('YYYY');
                    let shijian = moment(values.shijian).format('YYYY');
                    if (fsSjRqYear != shijian) {
                        this.$message.warning('请选择当年的日期！');
                        return;
                    }
                    httpAction(httpurl,formData,method).then((res)=>{
                        if(res.success){
                            that.$message.success(res.message);
                            that.$emit('ok');
                        }else{
                            that.$message.warning(res.message);
                        }
                        }).finally(() => {
                        that.confirmLoading = false;
                    })
                }
            })
        },
        popupCallback(row){
            this.form.setFieldsValue(pick(row,'fsSjXh','fsSjBh','fsSjRq','fsSjSxw','shijian','isValid','bz1','bz2'))
        },
    }
  }
</script>